import {
  addStudent,
  deleteStudent,
  queryStudent,
  editPassword,
  editStudent,
} from '../services/student';

export default {
  namespace: 'student',

  state: {
    studentList: [],
  },

  effects: {
    *fetchStudentList({ payload }, { call, put }) {
      const dataLsit = yield call(queryStudent, payload);
      if (dataLsit.status) {
        yield put({
          type: 'saveStudentList',
          payload: dataLsit.data,
        });
      }
    },
    *addStudent({ payload }, { call, put }) {
      const data = yield call(addStudent, payload);
      if (data.status) {
        const dataLsit = yield call(queryStudent, {});
        yield put({
          type: 'saveStudentList',
          payload: dataLsit.data,
        });
      }
    },
    *removeStudent({ payload }, { call, put }) {
      const data = yield call(deleteStudent, payload);
      if (data.status) {
        const dataLsit = yield call(queryStudent, {});
        yield put({
          type: 'saveStudentList',
          payload: dataLsit.data,
        });
      }
    },
    *updateStudent({ payload }, { call, put }) {
      const data = yield call(editStudent, payload);
      if (data.status) {
        const dataLsit = yield call(queryStudent, {});
        yield put({
          type: 'saveStudentList',
          payload: dataLsit.data,
        });
      }
    },
    *updatePassword({ payload }, { call, put }) {
      const data = yield call(editPassword, payload);
      if (data.status) {
        yield put({
          type: '',
          payload: '',
        });
      }
    },
  },

  reducers: {
    saveStudentList(state, { payload }) {
      return {
        ...state,
        studentList: payload,
      };
    },
  },
};
